from contextlib import contextmanager
import datetime


def log(string, logger=None):
    if logger is None:
        print(string)
    else:
        logger.info(string)


@contextmanager
def timeit(logger=None):
    start_t = datetime.datetime.now()
    log(f"start :{start_t}", logger)
    try:
        yield
    finally:
        end_t = datetime.datetime.now()
        log(f"end :{end_t}", logger)
        duration_t = end_t - start_t
        log(f"duration :{duration_t}", logger)